.\" $OpenBSD: PEM_bytes_read_bio.3,v 1.6 2020/07/23 17:34:53 schwarze Exp $
.\" selective merge up to:
.\" OpenSSL PEM_bytes_read_bio.pod 7671342e Feb 29 15:47:12 2016 -0600
.\"
.\" This file is a derived work.
.\" The changes are covered by the following Copyright and license:
.\" Copyright (c) 2020 Ingo Schwarze <schwarze@openbsd.org>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.\" The original file was written by Benjamin Kaduk <bkaduk at akamai dot com>.
.\" Copyright (c) 2017 The OpenSSL Project.  All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in
.\"    the documentation and/or other materials provided with the
.\"    distribution.
.\"
.\" 3. All advertising materials mentioning features or use of this
.\"    software must display the following acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
.\"
.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
.\"    endorse or promote products derived from this software without
.\"    prior written permission. For written permission, please contact
.\"    openssl-core@openssl.org.
.\"
.\" 5. Products derived from this software may not be called "OpenSSL"
.\"    nor may "OpenSSL" appear in their names without prior written
.\"    permission of the OpenSSL Project.
.\"
.\" 6. Redistributions of any form whatsoever must retain the following
.\"    acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: July 23 2020 $
.Dt PEM_BYTES_READ_BIO 3
.Os
.Sh NAME
.Nm PEM_bytes_read_bio
.Nd read a PEM-encoded data structure from a BIO
.Sh SYNOPSIS
.In openssl/pem.h
.Ft int
.Fo PEM_bytes_read_bio
.Fa "unsigned char **pdata"
.Fa "long *plen"
.Fa "char **pnm"
.Fa "const char *name"
.Fa "BIO *in_bp"
.Fa "pem_password_cb *cb"
.Fa "void *u"
.Fc
.Sh DESCRIPTION
.Fn PEM_bytes_read_bio
reads and PEM decodes the first object of type
.Fa name
.Pq e.g. RSA PRIVATE KEY, CERTIFICATE, etc.\&
from
.Fa in_bp .
If multiple PEM-encoded data structures are present in the same stream,
it skips non-matching data types and continues reading.
Before reading each PEM object, lines not starting with
.Qq "-----BEGIN "
are also skipped; see
.Xr PEM_read_bio 3
for details of PEM parsing.
.Pp
The PEM header may indicate that the following data is encrypted; if so,
the data is decrypted, optionally using
.Fa cb
and
.Fa u ,
as described in
.Xr pem_password_cb 3 .
.Pp
Some data types have compatibility aliases, such as a file containing
X509 CERTIFICATE matching a request for the deprecated type CERTIFICATE.
The actual type indicated by the file is returned in
.Em *pnm
if
.Fa pnm
is
.Pf non- Dv NULL .
The caller must free the storage pointed to by
.Em *pnm .
.Pp
The returned data is the DER-encoded form of the requested type, in
.Em *pdata
with length
.Em *plen .
The caller must free the storage pointed to by
.Em *pdata .
.Sh RETURN VALUES
.Fn PEM_bytes_read_bio
returns 1 for success or 0 for failure.
.Sh ERRORS
Diagnostics that can be retrieved with
.Xr ERR_get_error 3 ,
.Xr ERR_GET_REASON 3 ,
and
.Xr ERR_reason_error_string 3
include:
.Bl -tag -width Ds
.It Dv PEM_R_NO_START_LINE Qq no start line
No more PEM objects were found in the input.
This can happen when the input contains no PEM objects at all,
or only objects that do not match the type
.Fa name .
.It Dv PEM_R_NOT_PROC_TYPE Qq not proc type
The first PEM header does not start with
.Qq "Proc-Type: " .
.It Dv PEM_R_NOT_ENCRYPTED Qq not encrypted
The Proc-Type header differs from
.Qq 4,ENCRYPTED .
.It Dv PEM_R_SHORT_HEADER Qq short header
The Proc-Type header is the last header line.
.It Dv PEM_R_NOT_DEK_INFO Qq not dek info
The second PEM header does not start with
.Qq "DEK-Info: " .
.It Dv PEM_R_UNSUPPORTED_ENCRYPTION Qq unsupported encryption
The cipher name given in the DEK-Info header is unknown to
.Xr EVP_get_cipherbyname 3 .
.It Dv PEM_R_BAD_IV_CHARS Qq "bad iv chars"
The word following the cipher name in the DEK-Info header
contains bytes that are not hexadecimal digits.
This also happens when the initialization vector is missing or too short.
.It Dv PEM_R_BAD_PASSWORD_READ Qq bad password read
.Fa cb
reported failure.
This may for example happen when the user mistypes the password.
.It Dv PEM_R_BAD_DECRYPT Qq bad decrypt
.Xr EVP_DecryptInit_ex 3 ,
.Xr EVP_DecryptUpdate 3 ,
or
.Xr EVP_DecryptFinal_ex 3
failed.
.El
.Pp
Additional types of errors can result from
.Xr PEM_read_bio 3 .
.Sh SEE ALSO
.Xr PEM_ASN1_read 3 ,
.Xr PEM_read 3 ,
.Xr PEM_read_bio_PrivateKey 3 ,
.Xr PEM_X509_INFO_read 3
.Sh STANDARDS
RFC 1421: Privacy Enhancement for Internet Electronic Mail (PEM), Part I
.Sh HISTORY
.Fn PEM_bytes_read_bio
first appeared in OpenSSL 0.9.7 and has been available since
.Ox 3.2 .
